package windows;

//import keyListener;
import java.awt.Dimension;
import java.awt.Toolkit;

import data.Angajat;
import data.AngajatiData;
import data.Scoala;
import handler.LoadData;
import handler.MyActionListener;
import handler.MyWindowsListener;
import handler.SaveData;
import handler.keyListener;
import handler.listSelectionListener;

public class Ferestre {
	AngajatiData angajati;

	private MainFrame mainFrame = new MainFrame();

	private ActionFrame actionFrame = new ActionFrame();

	private AddAngajatFrame addAngajatFrame = new AddAngajatFrame();

	private RemoveAngajatFrame removeAngajatFrame = new RemoveAngajatFrame();

	private ModifyAngajatiFrame modifyAngajatiFrame = new ModifyAngajatiFrame();

	private ReportGeneratorFrame reportGeneratorFrame = new ReportGeneratorFrame();
	
	private AddSchoolFrame addSchoolFrame = new AddSchoolFrame();
	
	private ModifySchoolFrame modifySchoolFrame = new ModifySchoolFrame();

	private LoadData load = new LoadData();

	public LoadData getLoad() {
		return load;
	}

	private SaveData save = new SaveData();

	public ActionFrame getActionFrame() {
		return actionFrame;
	}

	public AddAngajatFrame getAddAngajatFrame() {
		return addAngajatFrame;

	}

	public AngajatiData getAngajatiData() {
		return angajati;
	}

	public MainFrame getMainFrame() {

		return mainFrame;
	}

	public ModifyAngajatiFrame getModifyFrame() {
		return modifyAngajatiFrame;

	}

	public RemoveAngajatFrame getRemoveAngajatFrame() {
		return removeAngajatFrame;

	}

	public ReportGeneratorFrame getReportGeneratorFrame() {
		return reportGeneratorFrame;
	}

	public SaveData getSave() {
		return save;
	}

	public void removeComboItems() {
		if (removeAngajatFrame.getComboBox_RemoveNumeAngajat().getItemCount() > 0) {
			removeAngajatFrame.getComboBox_RemoveNumeAngajat().removeItemAt(0);
			removeComboItems();
		}
	}

	public void run() {
		load.LoadAngajati();

		angajati = load.getlistaScoli();
		mainFrame.setFrame();
		setMainFrame();

		addAngajatFrame.getComboBox_BancaAngajat().addItem("Raffeisen Bank");
		addAngajatFrame.getComboBox_BancaAngajat()
				.addItem("Banca Transilvania");

	}

	public void setActionFrame() {

		MyWindowsListener actionFrameListener = new MyWindowsListener(this);
		actionFrame.getFrame().addWindowListener(actionFrameListener);
		if (actionFrame.isOpenedFirstTime()) {
			MyActionListener addWorkerButtonListener = new MyActionListener(
					this);
			MyActionListener removeWorkerButtonListener = new MyActionListener(
					this);
			MyActionListener modifyWorkersButtonListener = new MyActionListener(
					this);
			MyActionListener reportGeneratorButtonistener = new MyActionListener(
					this);

			actionFrame.getAddWorkerButton().addActionListener(
					addWorkerButtonListener);
			actionFrame.getRemoveWorkerButton().addActionListener(
					removeWorkerButtonListener);
			actionFrame.getModifyWorkersButton().addActionListener(
					modifyWorkersButtonListener);

			actionFrame.getReportGeneratorButton().addActionListener(
					reportGeneratorButtonistener);
		}
		 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)actionFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)actionFrame.getFrame().getSize().getHeight()/2;
		 actionFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);
	}

	public void setAddAngajatFrame() {
		MyWindowsListener addAngajatFrameListener = new MyWindowsListener(this);
		addAngajatFrame.getFrame().addWindowListener(addAngajatFrameListener);
		if (addAngajatFrame.isOpenedFirstTime()) {
			MyActionListener saveWorkerButtonListener = new MyActionListener(
					this);
			MyActionListener cancelWorkerButtonListener = new MyActionListener(
					this);

			addAngajatFrame.getSaveAngajatButton().addActionListener(
					saveWorkerButtonListener);
			addAngajatFrame.getCancelAngajatButton().addActionListener(
					cancelWorkerButtonListener);
		}
		 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)addAngajatFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)addAngajatFrame.getFrame().getSize().getHeight()/2;
		 addAngajatFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);

	}

	public void setMainFrame() {
		MyActionListener selectSchoolButtonListener = new MyActionListener(this);
		MyActionListener addSchoolButotnListener = new MyActionListener(this);
		MyActionListener modifySchoolListener = new MyActionListener(this);
		MyActionListener removeSchoolButtonListener = new MyActionListener(this);
		MyActionListener loadDatabaseButtonListener = new MyActionListener(this);
		MyActionListener exportDatabaseButtonListener = new MyActionListener(
				this);
		
		
		keyListener mainKey = new keyListener(this);
		
		mainFrame.getAddSchoolButton().addActionListener(
				addSchoolButotnListener);
		mainFrame.getModifySchool().addActionListener(modifySchoolListener);
		mainFrame.getRemoveSchoolButton().addActionListener(
				removeSchoolButtonListener);
		mainFrame.getOkButton().addActionListener(selectSchoolButtonListener);
		mainFrame.getLoadDatabaseButton().addActionListener(
				loadDatabaseButtonListener);
		mainFrame.getExportDatabaseButton().addActionListener(
				exportDatabaseButtonListener);
		
	//	mainFrame.getTextField_SchoolName().addKeyListener(mainKey);
		
		mainFrame.getAddSchoolButton().addKeyListener(mainKey);
		mainFrame.getComboBox_Scoala().addKeyListener(mainKey);
		mainFrame.getOkButton().addKeyListener(mainKey);
		mainFrame.getRemoveSchoolButton().addKeyListener(mainKey);
		
		for (Scoala school : angajati.getListaScoli())
			mainFrame.getComboBox_Scoala().addItem(school.getNumeScoala());
		
		
		 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)mainFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)mainFrame.getFrame().getSize().getHeight()/2;
         mainFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);
		
	}

	public void setModifyFrame() {
		modifyAngajatiFrame.getTabelAngajati().setFocusCycleRoot(true);
		MyWindowsListener modifyAngajatFrameListener = new MyWindowsListener(
				this);
		modifyAngajatiFrame.getFrame().addWindowListener(
				modifyAngajatFrameListener);
		keyListener key=new keyListener(this);
		listSelectionListener cellSelected=new listSelectionListener(this);
		
		modifyAngajatiFrame.getTabelAngajati().addKeyListener(key);
		modifyAngajatiFrame.getTabelAngajati().getSelectionModel().addListSelectionListener(cellSelected);
		if (modifyAngajatiFrame.isOpenedFirstTime()) {
			MyActionListener saveChangesListener = new MyActionListener(this);
			modifyAngajatiFrame.getModificaAngajat().addActionListener(
					saveChangesListener);
		}
		 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)modifyAngajatiFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)modifyAngajatiFrame.getFrame().getSize().getHeight()/2;
		 modifyAngajatiFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);
	}

	public void setRemoveAngajatFrame() {
		Angajat employer;
		MyWindowsListener removeAngajatFrameListener = new MyWindowsListener(
				this);
		removeAngajatFrame.getFrame().addWindowListener(
				removeAngajatFrameListener);

		removeAngajatFrame.getPrevButton().setEnabled(false);
		if (angajati.getSelectedSchool().getListaAngajati().size() != 0) {
			if (angajati.getSelectedSchool().getListaAngajati().size() == 1) {
				removeAngajatFrame.getNextButton().setEnabled(false);
			}

			MyActionListener changeItem = new MyActionListener(this);

			removeAngajatFrame.getComboBox_RemoveNumeAngajat()
					.addActionListener(changeItem);
			for (Angajat worker : angajati.getSelectedSchool()
					.getListaAngajati()) {
				removeAngajatFrame.getComboBox_RemoveNumeAngajat().addItem(
						worker.getNumeAngajat());
			}
			removeAngajatFrame.getComboBox_RemoveNumeAngajat()
					.setSelectedIndex(0);
			employer = angajati.getSelectedSchool().getListaAngajati().get(0);
			removeAngajatFrame.getTextField_RemoveContAngajat().setText(
					employer.getContAngajat());
			removeAngajatFrame.getTextField_RemoveSumaAngajat().setText(
					employer.getSuma().toString());
			removeAngajatFrame.getTextField_RemoveBancaAngajat().setText(
					employer.getBancaAngajat());
			removeAngajatFrame.getDeleteAngajatButton().setEnabled(true);
		} else {
			removeAngajatFrame.getNextButton().setEnabled(false);
			removeAngajatFrame.getDeleteAngajatButton().setEnabled(false);
		}
		if (removeAngajatFrame.isOpenedFirstTime()) {
			MyActionListener nextEmployer = new MyActionListener(this);
			MyActionListener prevEmployer = new MyActionListener(this);
			MyActionListener deleteEmployer = new MyActionListener(this);

			removeAngajatFrame.getNextButton().addActionListener(nextEmployer);
			removeAngajatFrame.getPrevButton().addActionListener(prevEmployer);
			removeAngajatFrame.getDeleteAngajatButton().addActionListener(
					deleteEmployer);

		}
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)removeAngajatFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)removeAngajatFrame.getFrame().getSize().getHeight()/2;
		 removeAngajatFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);
		
	}
	public void setReportGeneratorFrame()
	{
		MyActionListener exportFiles=new MyActionListener(this);
		reportGeneratorFrame.getReportGenerator().addActionListener(exportFiles);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		 int horizontalPosition,VerticalPosition;
		 horizontalPosition=(int)d.getWidth()/2 - (int)reportGeneratorFrame.getFrame().getSize().getWidth()/2;
		 VerticalPosition=(int)d.getHeight()/2 - (int)reportGeneratorFrame.getFrame().getSize().getHeight()/2;
		 reportGeneratorFrame.getFrame().setLocation(horizontalPosition,VerticalPosition);
	}

	public void setAddSchoolFrame() {
		MyActionListener addSchoolListener = new MyActionListener(this);
		MyActionListener cancelAddSchoolListener = new MyActionListener(this);
		
		addSchoolFrame.getAddSchoolButton().addActionListener(addSchoolListener);
		addSchoolFrame.getCancelSchoolButton().addActionListener(cancelAddSchoolListener);
	}

	public AddSchoolFrame getAddSchoolFrame() {
		return addSchoolFrame;
	}

	

	public ModifySchoolFrame getModifySchoolFrame() {
		return modifySchoolFrame;
	}

	public void setModifySchoolFrame() {
		MyActionListener saveSchoolListener =new MyActionListener(this);
		MyActionListener cancelSchoolListener = new MyActionListener(this);
		
		modifySchoolFrame.getModifySchoolButton().addActionListener(saveSchoolListener);
		modifySchoolFrame.getCancelSchoolButton().addActionListener(cancelSchoolListener);
		
	}

}
